package com.iris.smartclass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iris.smartclass.common.BaseResponse;
import com.iris.smartclass.common.ErrorCode;
import com.iris.smartclass.common.ResultUtils;
import com.iris.smartclass.exception.BusinessException;
import com.iris.smartclass.exception.ThrowUtils;
import com.iris.smartclass.mapper.SituationMapper;
import com.iris.smartclass.model.dto.situation.SituationAddRequest;
import com.iris.smartclass.model.dto.situation.SituationDeleteRequest;
import com.iris.smartclass.model.dto.situation.SituationUpdateRequest;
import com.iris.smartclass.model.entity.Situation;
import com.iris.smartclass.model.entity.User;
import com.iris.smartclass.model.vo.SituationVO;
import com.iris.smartclass.service.SituationService;
import com.iris.smartclass.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/situation")
public class SituationController {

    @Resource
    private SituationService situationService;

    @Resource
    private SituationMapper situationMapper;

    @Resource
    private UserService userService;


    @GetMapping("/getAllSituation")
    public BaseResponse<List<SituationVO>> getAllSituation(HttpServletRequest request) {
        return ResultUtils.success(situationService.getSituationVOList(request));
    }

    @PostMapping("/add")
    public BaseResponse<Long> addSituation(@RequestBody SituationAddRequest situationAddRequest, HttpServletRequest request) {
        if (situationAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long uuid = loginUser.getId();
        Situation situation = new Situation();
        BeanUtils.copyProperties(situationAddRequest, situation);
        situation.setUuid(uuid);
        boolean result = situationService.save(situation);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(situation.getId());
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteSituation(@RequestBody SituationDeleteRequest deleteSituationRequest, HttpServletRequest request) {
        if (deleteSituationRequest == null || deleteSituationRequest.getStId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Situation> deleteRequest = new QueryWrapper<>();
        deleteRequest.eq("stId", deleteSituationRequest.getStId());
        int delete = situationMapper.delete(deleteRequest);
        return ResultUtils.success(delete>0);
    }

    /**
     * 更新学生
     *
     * @param situationUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateSituation(@RequestBody SituationUpdateRequest situationUpdateRequest,
                                                 HttpServletRequest request) {
        if (situationUpdateRequest == null || situationUpdateRequest.getStId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Situation situation = new Situation();
        BeanUtils.copyProperties(situationUpdateRequest, situation);
        QueryWrapper<Situation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stId", situationUpdateRequest.getStId());
        boolean result = situationService.update(situation,queryWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

}
